import { ref } from "vue";
import { useStore } from "vuex";

/**
 * 发送验证码请求
 * @returns { sendMsg, phoneNumber, isSending }
 */
export function sendMsg() {
    const phoneNumberVerify1 = ref(""); // 手机号
    const isSending = ref(false); // 发送状态

    const store = useStore(); // 获取 Vuex Store 实例

    // 处理发送验证码
    const handleSendMsg = async () => {
        // 验证手机号
        if (!phoneNumberVerify1.value) {
            console.log('是否有手机号',phoneNumberVerify1.value);
            alert("请输入手机号");
            return;
        }
        isSending.value = true; // 设置发送状态为 true

        try {
            // 调用 Vuex 中的 sendMsg action
            const response = await store.dispatch("sendMsg", phoneNumberVerify1.value);
            if (response.success) {
                alert("验证码已发送");
            } else {
                alert(response.message || "发送失败");
            }
        } catch (error) {
            console.error("发送验证码错误:", error);
            alert("发送验证码失败，请检查网络或稍后再试");
        } finally {
            isSending.value = false; // 重置发送状态
        }
    };

    return {
        handleSendMsg,
        phoneNumberVerify1,
        isSending,
    };
}
